//
//  AppDelegate.swift
//  daoge_sineweibo
//
//  Created by 伍禄林 on 16/9/26.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        
        setupAdditions()
        
        window = UIWindow()
        
        window?.backgroundColor = UIColor.white;
        
        let vc = WBMainTabBarController();
        
        window?.rootViewController = vc
        
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        
        return true
    }

   

}

extension AppDelegate {
    
    func setupAdditions()  {
        
        //设置最小显示时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        //设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //设置用户授权显示通知
        if #available(iOS 10.0, *) { //10.0以上的版本采用这种方式请求用户授权
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {//10.0一下请求用户授权的方式
            let nofitySettngs = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            
            UIApplication.shared.registerUserNotificationSettings(nofitySettngs)
            
        }

        
    }
    
}



extension AppDelegate {
    
    func loadAppInfo()  {
        
        DispatchQueue.global().async {
            
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            
            let data = NSData(contentsOf: url!)
            
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last
            
            let jsonPath = docDir?.appending("/main.json")
            
            data?.write(toFile: jsonPath!, atomically: true)
            
        }
        
        
    }
    
    
}

